package com.luyuan.crm.sms.service.impl;

import java.util.Date;

import com.google.gdata.util.common.base.StringUtil;
import com.luyuan.crm.sms.Constant;
import com.luyuan.crm.sms.command.SendSmsCommand;
import com.luyuan.crm.sms.model.SmsLogModel;
import com.luyuan.crm.sms.service.SmsLogService;
import com.luyuan.crm.sms.service.SmsService;
import com.luyuan.crm.sms.task.CommandExecutor;
import com.luyuan.crm.sms.task.SmsTaskListener;
import com.ruijie.components.setting.service.SettingService;
import com.ruijie.foundation.security.AppUserContext;

/*******************************
 * 2012-7-18
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * 
 * @author chenqibo
 * @version 1.0
 ********************************/
public class SmsServiceImpl implements SmsService {

    private SettingService settingService;

    private CommandExecutor tastExecutor;

    private SmsLogService logService;

    @Override
    public void sendMessage(final String phone, final String content) {

        String cpid = settingService.getSetting(Constant.CP_ID).getValue();
        String cppwd = settingService.getSetting(Constant.CP_PWD).getValue();
        String cpsign = settingService.getSetting(Constant.CP_SIGN).getValue();
        String userName = AppUserContext.getCurrentUsername();

        final String spnumber = String.valueOf(System.currentTimeMillis());
        final String u = userName;

        SendSmsCommand command = new SendSmsCommand(phone, content + cpsign, spnumber, "", cpid, cppwd);
        if (StringUtil.isEmpty(userName)) {
            userName = "系统";
        }

        this.tastExecutor.execute(command, new SmsTaskListener() {

            @Override
            public void onError(Exception e) {
                // 记录日志
                SmsLogModel model = new SmsLogModel();
                model.setContent(content);
                model.setStatus(false);
                model.setDetail(e.getMessage());
                model.setSendDate(new Date());
                model.setFromUsername(u);
                model.setToPhone(phone);
                // model.setToUserName(toUserName);
                model.setSpnumber(spnumber);
                logService.recordLog(model);
            }

            @Override
            public void onSuccess(Object[] result) {

                if (result != null && result.length > 0) {
                    // 记录日志
                    SmsLogModel model = new SmsLogModel();
                    if ("0".equals(result[0].toString())) {
                        model.setStatus(true);
                        model.setDetail("发送成功");
                    }
                    else {
                        model.setDetail(result[0].toString());
                        model.setStatus(false);
                    }
                    model.setContent(content);
                    model.setSendDate(new Date());
                    model.setFromUsername(u);
                    model.setToPhone(phone);
                    // model.setToUserName(toUserName);
                    model.setSpnumber(spnumber);
                    logService.recordLog(model);
                }
            }
        });
    }

    public void setLogService(SmsLogService logService) {
        this.logService = logService;
    }

    public void setSettingService(SettingService settingService) {
        this.settingService = settingService;
    }

    public void setTastExecutor(CommandExecutor tastExecutor) {
        this.tastExecutor = tastExecutor;
    }
}
